package com.game.centerserver.controller;

import com.game.centerserver.common.Result;
import com.game.centerserver.infrastructure.entity.ServerMasterInfo;
import com.game.centerserver.infrastructure.service.ServerMasterInfoService;
import com.game.centerserver.pojo.result.HttpStateCodeResult;
import com.game.centerserver.pojo.result.ServerMasterView;
import com.game.centerserver.pojo.result.ServerStateView;
import com.game.centerserver.service.HttpClientService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// MasterServerInfo的相关接口
@RestController
@RequestMapping("/servers")
public class MasterServerController {

    @Resource
    ServerMasterInfoService serverMasterInfoService;

    @Resource
    HttpClientService httpClientService;

    @GetMapping("page")
    public Result page() {
        return Result.fail("fail");
    }

    @GetMapping("deleteServerByIP")
    public Result deleteServer(String IP) {
        boolean delete = serverMasterInfoService.deleteById(IP);
        if(delete){
            return Result.ok("删除成功");
        }else{
            return Result.fail("删除失败");
        }

    }

    @GetMapping("getServerByIP")
    public Result<ServerMasterView> getSeverByIP(String IP) {
        ServerMasterInfo serverMasterInfo = serverMasterInfoService.queryById(IP);
        if (serverMasterInfo != null){
            return Result.ok("查询成功",new ServerMasterView(serverMasterInfo));
        }else{
            return Result.fail("查询失败");
        }

    }

    @PutMapping("addServer")
    public Result addServer(ServerMasterView serverMasterView) {
        boolean insert = false;
        try {
            insert = serverMasterInfoService.insert(new ServerMasterInfo(serverMasterView));
            if (insert){
                return Result.ok("插入成功");
            }else{
                return Result.fail("插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("插入失败");
        }

    }

    @PutMapping("updataServer")
    public Result updataServerByIP(ServerMasterView serverMasterView) {
        try {
            boolean update = serverMasterInfoService.update(new ServerMasterInfo(serverMasterView));
            if(update){
                return Result.ok("更新成功");
            }else{
                return Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }

    @GetMapping("getStatusByIP")
    public Result<ServerStateView> getStatusByIP(String ip) {
        ServerMasterInfo serverMasterInfo = serverMasterInfoService.queryById(ip);
        if (serverMasterInfo == null){
            return Result.fail("获取端口失败");
        }
        HttpStateCodeResult<ServerStateView> systemState = httpClientService.getSystemState(ip, serverMasterInfo.getPort());
        if (systemState.getCode() == 500){
            return Result.fail("获取服务器状态信息失败");
        }
        return Result.ok("获取服务器状态信息成功",systemState.getPayload());
    }

}
